/// Elegance控件库的标签控件
///
/// 简洁的标签组件，支持不同样式、尺寸和功能

import 'package:flutter/material.dart';

import '../../core/elegance_theme.dart';

/// 标签主题类型枚举
enum ETagType {
  /// 主要样式
  primary,

  /// 成功样式
  success,

  /// 信息样式
  info,

  /// 警告样式
  warning,

  /// 错误样式
  error,
}

/// 标签大小枚举
enum ETagSize {
  /// 大型标签
  large,

  /// 中型标签
  medium,

  /// 小型标签
  mini,
}

/// 标签形状枚举
enum ETagShape {
  /// 方形标签
  square,

  /// 圆形标签
  circle,
}

/// ETag控件
class ETag extends StatelessWidget {
  /// 创建ETag控件
  const ETag({
    super.key,
    required this.text,
    this.type = ETagType.primary,
    this.size = ETagSize.medium,
    this.shape = ETagShape.square,
    this.plain = false,
    this.plainFill = false,
    this.disabled = false,
    this.closable = false,
    this.bgColor,
    this.color,
    this.borderColor,
    this.closeColor,
    this.name,
    this.icon,
    this.onClick,
    this.onClose,
  });

  /// 标签的文字内容
  final String text;

  /// 主题类型
  final ETagType type;

  /// 标签大小
  final ETagSize size;

  /// 标签形状
  final ETagShape shape;

  /// 是否镂空
  final bool plain;

  /// 镂空时是否填充背景色
  final bool plainFill;

  /// 是否禁用
  final bool disabled;

  /// 是否可关闭，设置为true，文字右边会出现一个关闭图标
  final bool closable;

  /// 背景颜色，默认为空字符串，即不处理
  final Color? bgColor;

  /// 标签字体颜色，默认为空字符串，即不处理
  final Color? color;

  /// 标签的边框颜色
  final Color? borderColor;

  /// 关闭按钮图标的颜色
  final Color? closeColor;

  /// 点击时返回的索引值，用于区分遍历的数组哪个元素被点击了
  final dynamic name;

  /// 内置图标，或绝对路径的图片
  final IconData? icon;

  /// 点击标签触发的回调
  final Function(dynamic name)? onClick;

  /// 点击关闭按钮触发的回调
  final Function(dynamic name)? onClose;

  @override
  Widget build(BuildContext context) {
    final theme = EleganceTheme.of(context);

    // 获取标签样式相关属性
    final styleProps = _getStyleProperties(theme);

    // 获取尺寸相关属性
    final sizeProps = _getSizeProperties();

    // 获取圆角属性
    final borderRadius = _getBorderRadius(theme);

    // 构建标签内容
    final content = Row(
      mainAxisSize: MainAxisSize.min,
      children: [
        if (icon != null) ...[
          Icon(icon, size: sizeProps.iconSize, color: styleProps.textColor),
          SizedBox(width: sizeProps.spacing),
        ],
        Text(
          text,
          style: TextStyle(
            color: styleProps.textColor,
            fontSize: sizeProps.fontSize,
            fontWeight: FontWeight.normal,
          ),
          overflow: TextOverflow.ellipsis,
          maxLines: 1,
        ),
        if (closable && !disabled) ...[
          SizedBox(width: sizeProps.spacing),
          Icon(
            Icons.close,
            size: sizeProps.closeIconSize,
            color: closeColor ?? styleProps.textColor,
          ),
        ],
      ],
    );

    // 构建标签容器
    final tagContainer = Container(
      padding: EdgeInsets.symmetric(
        horizontal: sizeProps.paddingHorizontal,
        vertical: sizeProps.paddingVertical,
      ),
      decoration: BoxDecoration(
        color: styleProps.bgColor,
        borderRadius: borderRadius,
        border: Border.all(
          color: styleProps.borderColor,
          width: disabled ? theme.borderWidthThin : theme.borderWidth,
        ),
      ),
      child: content,
    );

    // 判断是否可点击
    if (disabled) {
      return Opacity(opacity: 0.6, child: tagContainer);
    }

    // 处理点击事件
    return GestureDetector(
      onTap: () {
        if (closable) {
          // 如果标签可关闭，点击标签本身触发onClick事件
          onClick?.call(name);
        } else {
          // 如果标签不可关闭，点击标签触发onClick事件
          onClick?.call(name);
        }
      },
      child: tagContainer,
    );
  }

  // 获取标签样式相关属性
  _StyleProperties _getStyleProperties(EleganceThemeData theme) {
    // 根据类型获取基础颜色
    Color baseColor;
    switch (type) {
      case ETagType.primary:
        baseColor = theme.primaryColor;
        break;
      case ETagType.success:
        baseColor = theme.successColor;
        break;
      case ETagType.info:
        baseColor = theme.infoColor;
        break;
      case ETagType.warning:
        baseColor = theme.warningColor;
        break;
      case ETagType.error:
        baseColor = theme.errorColor;
        break;
      default:
        baseColor = theme.primaryColor;
    }

    // 处理禁用状态
    if (disabled) {
      return _StyleProperties(
        bgColor: color != null ? color!.withOpacity(0.1) : theme.surfaceColor,
        textColor: color ?? theme.textSecondaryColor,
        borderColor: borderColor ?? theme.borderColor,
      );
    }

    // 处理自定义颜色
    if (bgColor != null && color != null && borderColor != null) {
      return _StyleProperties(
        bgColor: bgColor!,
        textColor: color!,
        borderColor: borderColor!,
      );
    }

    // 处理镂空样式
    if (plain) {
      if (plainFill) {
        // 镂空带背景色
        return _StyleProperties(
          bgColor: baseColor.withOpacity(0.1),
          textColor: color ?? baseColor,
          borderColor: borderColor ?? baseColor,
        );
      } else {
        // 纯镂空
        return _StyleProperties(
          bgColor: Colors.transparent,
          textColor: color ?? baseColor,
          borderColor: borderColor ?? baseColor,
        );
      }
    }

    // 默认样式
    return _StyleProperties(
      bgColor: bgColor ?? baseColor,
      textColor: color ?? theme.textPrimaryColor,
      borderColor: borderColor ?? baseColor,
    );
  }

  // 获取尺寸相关属性
  _SizeProperties _getSizeProperties() {
    switch (size) {
      case ETagSize.large:
        return _SizeProperties(
          fontSize: 15,
          iconSize: 15,
          closeIconSize: 13,
          paddingHorizontal: 12,
          paddingVertical: 6,
          spacing: 4,
        );
      case ETagSize.medium:
        return _SizeProperties(
          fontSize: 14,
          iconSize: 14,
          closeIconSize: 12,
          paddingHorizontal: 10,
          paddingVertical: 4,
          spacing: 3,
        );
      case ETagSize.mini:
        return _SizeProperties(
          fontSize: 12,
          iconSize: 12,
          closeIconSize: 10,
          paddingHorizontal: 8,
          paddingVertical: 2,
          spacing: 2,
        );
      default:
        return _SizeProperties(
          fontSize: 14,
          iconSize: 14,
          closeIconSize: 12,
          paddingHorizontal: 10,
          paddingVertical: 4,
          spacing: 3,
        );
    }
  }

  // 获取圆角属性
  BorderRadius _getBorderRadius(EleganceThemeData theme) {
    switch (shape) {
      case ETagShape.circle:
        return BorderRadius.circular(theme.borderRadiusFull);
      case ETagShape.square:
      default:
        return BorderRadius.circular(theme.borderRadius);
    }
  }
}

// 标签样式属性类
class _StyleProperties {
  final Color bgColor;
  final Color textColor;
  final Color borderColor;

  _StyleProperties({
    required this.bgColor,
    required this.textColor,
    required this.borderColor,
  });
}

// 标签尺寸属性类
class _SizeProperties {
  final double fontSize;
  final double iconSize;
  final double closeIconSize;
  final double paddingHorizontal;
  final double paddingVertical;
  final double spacing;

  _SizeProperties({
    required this.fontSize,
    required this.iconSize,
    required this.closeIconSize,
    required this.paddingHorizontal,
    required this.paddingVertical,
    required this.spacing,
  });
}
